# 3.14 Servo ## 3.14.1 Overview The 9g servo features small size but high performance and precision and is with good torque and accuracy, so it is perfect for small machines. With up to 180 degrees rotation angle, it enables extremely precise rotation and control and can be started fast with low noise. ## 3.14.2 Schematic Diagram **Angle range:** 180°(there are 360°, 180° and 90°) **Drive voltage:** 3.3V / 5V Usually three wires: ![6-14](./media/6-14-2-1.png) **GND:** grounded, in brown **VCC:** connect to +5v (3.3V) power, in red **S:** signal pin to control PWM signal, in orange ![6-14](./media/6-14-2-2.png) **Control principle**: The rotation Angle of the servo is controlled by adjusting the duty cycle of the PWM (pulse width modulation) signals. Theoretically, the period of the standard PWM signal is fixed at 20ms (50Hz), so the pulse width should be 1ms ~ 2ms. But in fact, it is 0.5ms ~ 2.5ms, corresponding to the servo angle of 0° ~ 180°. Note that the angle for the same signal varies from servo brands. ## 3.14.3 Code Blocks 1. ![j43](./media/j43.png) This block controls servo angle. You only need to set a pin, channel, degree(range of 0-180) and delay(1000=1S). 2. ![j45](./media/j45.png) This block reads servo angle. 3. ![j6](./media/j6.png) This is a repeat block with repeating times. You can set the times. For instance, here it is 10, so codes in it will repeatedly execute for 10 times. ## 3.14.4 Test Code ### 3.14.4.1 Build Code There are two ways to upload the code: directly open the code file we provide; or manually build blocks. **Directly open the code file we provide:** 1. Click ![](./media/j68.png) and choose `Load from your computer` ![](./media/j67.png) 2. We have already downloaded the codes on computer desktop, so open the file and choose `3-14-servo.sb3` **Manually build blocks:** 1. In ![events](./media/events.png), find ![j1](./media/j1.png) block. 2. In ![control](./media/control.png), find and place ![j2](./media/j2.png) block under ![j1](./media/j1.png). ![6-1-4-1-1](./media/6-1-4-1-1.png) 3. In ![servo](./media/servo.png), put ![j43](./media/j43.png) in block ![j2](./media/j2.png) and set degree to “0” and delay to “2000”. 4. Duplicate the block twice and set degree to “90” and “180” respectively. **Complete Test Code** ![](./media/6-14-4-1.png) ### 3.14.4.2 Test Result After uploading code, the servo rotates to 0 degree, 90 degree and 180 degree accordingly, with each position staying for 2 seconds, and then it back to 0 degree. It repeats these rotations. ## 3.14.5 Extension We control servo to repeatedly and gradually rotate from 0 degree to 180 degree and then gradually back to 0 degree. ### 3.14.5.1 Test Code There are two ways to upload the code: directly open the code file we provide; or manually build blocks. **Directly open the code file we provide:** 1. Click ![](./media/j68.png) and choose `Load from your computer` ![](./media/j67.png) 2. We have already downloaded the codes on computer desktop, so open the file and choose `3-14-servo2.sb3` **Manually build blocks:** 1. In ![events](./media/events.png), find ![j1](./media/j1.png) block. 2. In ![control](./media/control.png), find and place ![j2](./media/j2.png) block under ![j1](./media/j1.png). ![6-1-4-1-1](./media/6-1-4-1-1.png) 3. In ![variable](./media/variable.png), drag ![j7](./media/j7.png) and put it under ![j1](./media/j1.png) ![6-18](./media/6-18-3-2-1.png) 4. In ![control](./media/control.png), drag ![j6](./media/j6.png) to set times to 180, and put it into ![j2](./media/j2.png) 5. In ![variable](./media/variable.png), put ![j10](./media/j10.png)into ![6-14](./media/6-14-4-1-2.png) ![6-14](./media/6-14-4-1-3.png) 6. In ![](./media/servo.png), drag and put ![](./media/j43.png) under ![j10](./media/j10.png) 7. In ![variable](./media/variable.png), set ![j8](./media/j8.png) to the degree in block ![](./media/j43.png), and set delay to 10 (unit: ms) ![6-14](./media/6-14-4-1-1.png) 8. Duplicate ![6-18](./media/6-14-4-1-2.png) and change “++” into “- -” in![j10](./media/j10.png) **Complete Test Code** ![6-14](./media/6-14-4-1-4.png) ### 3.14.5.1 Test Result After uploading code, the servo repeatedly and gradually rotates from 0 degree to 180 degree and then gradually back to 0 degree.